package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.MouseEvent;

public class HingeTool extends RectTool
{
	private var hinge_handler:Boolean;
	private var focusHinge:Hinge;
	
	public function HingeTool()
	{
		super();
		this.keyCode=72;//h
	}
	
	override public function drawSelectedPoint(point:Object,point_pen:Graphics,mouse_mc:Sprite):void{
		super.drawSelectedPoint(point,point_pen,mouse_mc);
		if(!point.isHinge)return;
		
		hinge_handler=true;
		//draw handler for rect setting
		var handler:Sprite=new Sprite();
		handler.graphics.lineStyle(0,0x0066cc,0);
		handler.graphics.beginFill(0x0066cc,1);
		handler.graphics.drawCircle(0,0,5);
		handler.graphics.endFill();
		
		var handler2:Sprite=new Sprite();
		handler2.graphics.lineStyle(0,0x0066cc,0);
		handler2.graphics.beginFill(0x0066cc,1);
		handler2.graphics.drawCircle(0,0,5);
		handler2.graphics.endFill();
		
		var handler3:Sprite=new Sprite();
		handler3.graphics.lineStyle(0,0x0066cc,0);
		handler3.graphics.beginFill(0x0066cc,1);
		handler3.graphics.drawCircle(0,0,5);
		handler3.graphics.endFill();
		
		var hinge:Hinge=canvas.getHinge(point);
		focusHinge=hinge;
		
		var corner:Array=canvas.getCorner(point);
		var p1:Object=canvas.data[corner[1]];
		var p2:Object=canvas.data[corner[2]];
		
		var dx1:Number=Math.abs(p1.x-p2.x);
		var dy1:Number=Math.abs(p1.y-p2.y);
		
		if(dx1<dy1){
			handler.x=point.x+hinge.distance;
			handler.y=point.y;
			
			handler2.x=point.x;
			handler2.y=point.y+hinge.height/2;
			
			handler3.x=point.x+hinge.distance;
			handler3.y=point.y+hinge.height/2+hinge.r;
		
		}else{
			handler.x=point.x;
			handler.y=point.y+hinge.distance;
			
			handler2.x=point.x-hinge.height/2;
			handler2.y=point.y;
			
			handler3.x=point.x-hinge.height/2-hinge.r;
			handler3.y=point.y+hinge.distance;
			
		}
		
		dx=dx1;
		dy=dy1;
		
		focusPoint=point;

		handler.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
		handler2.addEventListener(MouseEvent.MOUSE_DOWN,downHandler2);
		handler3.addEventListener(MouseEvent.MOUSE_DOWN,downHandler3);
		
		
		mouse_mc.addChild(handler);
		mouse_mc.addChild(handler2);
		mouse_mc.addChild(handler3);
	}
	
	public function downHandler3(evt:MouseEvent):void{
		caughtDown=true;
		resetting=true;
		canvas.stage.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler3);
		canvas.stage.addEventListener(MouseEvent.MOUSE_UP,upHandler);
	}
	override public function downHandler2(evt:MouseEvent):void{
		caughtDown=true;
		resetting=true;
		canvas.stage.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler2);
		canvas.stage.addEventListener(MouseEvent.MOUSE_UP,upHandler);
	}
	
	override public function downHandler(evt:MouseEvent):void{
		caughtDown=true;
		resetting=true;
		canvas.stage.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
		canvas.stage.addEventListener(MouseEvent.MOUSE_UP,upHandler);
	}
	
	override public function upAddPointAction():void{
		if(resetting)return;
		addPoint.id=canvas.getNewId();
		targetLine.splitPath(addPoint);
		
		//add the point, then make it a arc, and add arc to arc collection
		trace("addpoint action");
		canvas.addHinge(addPoint);
	}
	
	override public function upHandler(evt:MouseEvent):void{
		caughtDown=false;
		resetting=false;
		canvas.stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
		canvas.stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler2);
		canvas.stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler3);
		canvas.stage.removeEventListener(MouseEvent.MOUSE_UP,upHandler);
	}
	
	override public function moveHandler(evt:MouseEvent):void{
		var p:Object=canvas.simpleMousePoint();
		if(dx<dy){
			focusHinge.distance=p.x-focusPoint.x;
		}else{
			focusHinge.distance=p.y-focusPoint.y;
		}
	}
	
	override public function moveHandler2(evt:MouseEvent):void{
		var p:Object=canvas.simpleMousePoint();
		if(dx<dy){
			focusHinge.height=(p.y-focusPoint.y)*2;
		}else{
			focusHinge.height=(focusPoint.x-p.x)*2;
		}
	}
	
	public function moveHandler3(evt:MouseEvent):void{
		var p:Object=canvas.simpleMousePoint();
		
		var tp:Object;
		
		if(dx<dy){
			tp={x:focusPoint.x+focusHinge.distance,y:focusPoint.y+focusHinge.height/2};
			focusHinge.r=canvas.pointDistance(tp,p);
		}else{
			tp={x:focusPoint.x-focusHinge.height/2,y:focusPoint.y+focusHinge.distance};
			focusHinge.r=canvas.pointDistance(tp,p);
		}
	}
	
	override public function directUpHandler(target:Object):void{
		
		if(target.isHinge){
			var h:Hinge=canvas.getHinge(target);
			h.flip();
		}
		
	}
	
}

}

